/*
 * Copyright 2010 Pierre-Yves Ricau
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.excilys.capiq.server;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import org.jasig.cas.client.util.AssertionHolder;
import org.jasig.cas.client.util.CommonUtils;
import org.jasig.cas.client.validation.Assertion;

import com.excilys.capiq.client.LoginInfo;
import com.excilys.capiq.client.service.LoginService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class CasLoginServiceImpl extends RemoteServiceServlet implements LoginService {

    private static final long serialVersionUID = 1L;

    private String            casServerUrlPrefix;
    private String            serverName;
    private String            service;

    @Override
    public void init(ServletConfig config) throws ServletException {

        super.init(config);

        casServerUrlPrefix = config.getInitParameter("casServerUrlPrefix");
        CommonUtils.assertNotNull(casServerUrlPrefix, "casServerUrlPrefix cannot be null.");

        serverName = config.getInitParameter("serverName");
        CommonUtils.assertNotNull(serverName, "serverName cannot be null.");

        String serviceValidateSuffix = config.getInitParameter("serviceValidateSuffix");
        CommonUtils.assertNotNull(serviceValidateSuffix, "serviceValidateSuffix cannot be null.");

        service = serverName + serviceValidateSuffix;

    }

    @Override
    public LoginInfo login(String requestUri) {

        Assertion assertion = AssertionHolder.getAssertion();

        LoginInfo loginInfo = new LoginInfo();

        HttpServletResponse response = perThreadResponse.get();

        if (assertion != null) {
            loginInfo.setLoggedIn(true);
            String nickname = assertion.getPrincipal().getName();
            // nickname += assertion.getPrincipal().getAttributes().toString();
            loginInfo.setNickname(nickname);

            String returnUrl = response.encodeURL(serverName);
            loginInfo.setLogoutUrl(casServerUrlPrefix + "/logout?url=" + returnUrl);
        } else {
            loginInfo.setLoggedIn(false);
            String serviceUrl = response.encodeURL(service);
            String loginUrl = CommonUtils.constructRedirectUrl(casServerUrlPrefix + "/login", "service", serviceUrl,
                    false, false);
            loginInfo.setLoginUrl(loginUrl);
        }

        return loginInfo;
    }
}
